package cn.big.bigd.biocode.util;

import com.maxmind.geoip.LookupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetAddress;
/**
 * @ClassName: GeoIPv4
 * @Description: TODO
 * @Author: FanWang
 * @CreateDate: 2018/1/31 下午4:50
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018/1/31
 **/
public class GeoIPv4 {
    private static final Logger logger = LoggerFactory.getLogger(GeoIPv4.class);
    private static LookupService lookUp;
    static {
        try {
            lookUp = new LookupService(
                    GeoIPv4.class.getResource("/GeoLiteCity.dat").getFile(),
                    LookupService.GEOIP_MEMORY_CACHE);
            logger.info("GeoIP Database loaded: " + lookUp.getDatabaseInfo());
        } catch (IOException e) {
            logger.info("Could not load geo ip database: " + e.getMessage());
        }
    }

    public static GeoLocation getLocation(String ipAddress) {
        return GeoLocation.map(lookUp.getLocation(ipAddress));
    }

    public static GeoLocation getLocation(long ipAddress){
        return GeoLocation.map(lookUp.getLocation(ipAddress));
    }

    public static GeoLocation getLocation(InetAddress ipAddress){
        return GeoLocation.map(lookUp.getLocation(ipAddress));
    }
}